package servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Patient;
import model.PatientSearch;

/**
 * Servlet implementation class DetailsServlet
 */
@WebServlet("/DetailsServlet")
public class PatientServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PatientServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		String patientID = request.getParameter("patientID");
		int id =Integer.parseInt(patientID);
		
		PatientSearch s = new PatientSearch();
		Patient p = s.getPatientDetail(id); 
		
		List<String> allergies = s.getAllergies(id);
		List<List<String>> medications = s.getMedicines(id);
		List<List<String>> visits = s.getVisits(id);
		
		HttpSession session = request.getSession();
		
		session.setAttribute("patient", p);
		session.setAttribute("medications", medications);
		session.setAttribute("allergies", allergies);
		session.setAttribute("visits", visits);
		
		RequestDispatcher view = request.getRequestDispatcher("patient.jsp");
		view.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{

	}

}
